package com.hyg.common.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hyg.common.R;

/**
 * Author: huangyuguang
 * Date: 2022/5/6
 * 确认对话框
 */
public class ConfirmDialog extends Dialog implements View.OnClickListener {

    private TextView tvTitle;
    private TextView tvContent;
    private TextView tvOk;
    private TextView tvCancel;

    private String title;
    private String content;
    private String confirmStr;
    private String cancelStr;
    private OnConfirmClick onConfirmClick;
    private boolean canCancelable;
    private String belongContext;

    /**
     * @param context 上下文
     * @param content  内容
     * @param onConfirmClick   true: 可以点击外部关闭弹出  false:不可以关闭弹窗
     */
    public ConfirmDialog(@NonNull Context context, String content, OnConfirmClick onConfirmClick) {
        this(context,context.getString(R.string.lib_tip),content,context.getString(R.string.lib_sure),context.getString(R.string.lib_cancel),onConfirmClick);
    }

    /**
     * @param context  上下文
     * @param content  内容
     * @param confirmStr  确认文字
     * @param cancelStr  取消文字
     * @param onConfirmClick  回调
     */
    public ConfirmDialog(@NonNull Context context, String content, String confirmStr, String cancelStr, OnConfirmClick onConfirmClick) {
        this(context,context.getString(R.string.lib_tip),content,confirmStr,cancelStr,onConfirmClick,true);
    }

    /**
     * @param context 上下文
     * @param content 内容
     * @param confirmStr 确认文字
     * @param cancelStr  取消文字
     * @param onConfirmClick  回调
     * @param canCancelable true: 可以点击外部关闭弹出  false:不可以关闭弹窗
     */
    public ConfirmDialog(@NonNull Context context, String content, String confirmStr, String cancelStr, OnConfirmClick onConfirmClick, boolean canCancelable) {
        this(context,context.getString(R.string.lib_tip),content,confirmStr,cancelStr,onConfirmClick,canCancelable);
    }

    /**
     * @param context  上下文
     * @param title   标题
     * @param content  内容
     * @param confirmStr  确认文字
     * @param cancelStr  取消文字
     * @param onConfirmClick  回调
     */
    public ConfirmDialog(@NonNull Context context, String title, String content, String confirmStr, String cancelStr, OnConfirmClick onConfirmClick) {
        this(context,title,content,confirmStr,cancelStr,onConfirmClick,true);
    }

    /**
     *
     * @param context 上下文
     * @param title 标题
     * @param content  内容
     * @param confirmStr 确认文字
     * @param cancelStr 取消文字
     * @param onConfirmClick 回调
     * @param canCancelable true: 可以点击外部关闭弹出  false:不可以关闭弹窗
     */
    public ConfirmDialog(@NonNull Context context, String title, String content, String confirmStr, String cancelStr, OnConfirmClick onConfirmClick, boolean canCancelable) {
        super(context, R.style.ActionMiddleDialogStyle);
        this.belongContext = context.toString();
        this.title = title;
        this.content = content;
        this.confirmStr = confirmStr;
        this.cancelStr = cancelStr;
        this.onConfirmClick = onConfirmClick;
        this.canCancelable = canCancelable;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lib_dialog_confirm);

        tvTitle = findViewById(R.id.tv_title);
        tvContent = findViewById(R.id.tv_content);
        tvOk = findViewById(R.id.tv_ok);
        tvCancel = findViewById(R.id.tv_cancel);

        tvOk.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
        if(title != null){
            tvTitle.setText(title);
        }
        tvContent.setText(content);
        if(TextUtils.isEmpty(cancelStr)){
            tvCancel.setVisibility(View.GONE);
        }else {
            tvCancel.setText(cancelStr);
        }
        if(!TextUtils.isEmpty(confirmStr)){
            tvOk.setText(confirmStr);
        }
        setCanCancelable(canCancelable);
    }

    /**
     * 是否属于该Context
     */
    public boolean isBelongContext(Context context){
        return belongContext.equals(context.toString());
    }

    public ConfirmDialog setCanCancelable(boolean canCancelable) {
        setCancelable(canCancelable);
        setCanceledOnTouchOutside(canCancelable);
        return this;
    }

    @Override
    public void onClick(View v) {
        dismiss();
        int id = v.getId();
        if (id == R.id.tv_cancel) {
            onClick(false);
        }else if(id == R.id.tv_ok){
            onClick(true);
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if(onConfirmClick != null){
            onConfirmClick.dismiss();
        }
    }

    private void onClick(boolean isConfirm){
        if(onConfirmClick != null){
            onConfirmClick.result(isConfirm);
        }
    }

    public abstract static class OnConfirmClick{
        protected abstract void result(boolean isConfirm);
        protected void dismiss(){}
    }
}
